package org.createModel.singleton.demo4;

/**
 * 双重检查锁
 */
public class Singleton {
    private Singleton() {}

    // volatile 防止指令重排
    private static volatile Singleton instance;

    public static Singleton getInstance() {
        // 首次检查
        if (instance == null) {
            synchronized (Singleton.class) {
                // 二次判断
                if (instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}
